//
//  cartCell.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/14.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit

class CartCell:UITableViewCell{
    @IBOutlet var selectBtn:UIButton!
    @IBOutlet var deleteButton:UIButton!
    @IBOutlet var nameLabel:UILabel!
    @IBOutlet var priceLabel:UILabel!
    @IBOutlet var cartImageView:XZImageView!
    @IBOutlet var lineView:UIView!
    @IBOutlet var numberCountView:NumberCountView!
    @IBOutlet var leftConstraint:NSLayoutConstraint!
    @IBOutlet var rightConstraint:NSLayoutConstraint!
    @IBOutlet var deleteButtonWidth:NSLayoutConstraint!
    @IBOutlet var swipView:UIView!
     var indexPath:NSIndexPath?
    var cartItemSelectValueChangedEnclose:((NSIndexPath,Bool)->Void)?
    var cellDidEnterEditModel:((NSIndexPath,Bool)->Void)?
    var deleteButtonClickHandle:((NSIndexPath)->Void)?
    var cartItemSelected:Bool=false{
        didSet{
            if cartItemSelected{
                selectBtn.setBackgroundImage(UIImage(named: "down_checkbox"), forState: UIControlState.Normal)
            }
            else{
                selectBtn.setBackgroundImage(UIImage(named: "nor_checkbox"), forState: UIControlState.Normal)

            }
        }
    }
    @IBAction func deleteButtonClick(button:UIButton)->Void{
        deleteButtonClickHandle?(indexPath!)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
//        let ges = UIPanGestureRecognizer(target: self, action: "panThisCell:")
//        ges.delegate = self
//        swipView.addGestureRecognizer(ges)
        let rigthGes = UISwipeGestureRecognizer(target: self, action: "swipRight:")
        rigthGes.delegate = self
        swipView.addGestureRecognizer(rigthGes)
        deleteButton.backgroundColor = commonRedColor
        let leftGes = UISwipeGestureRecognizer(target: self, action: "swipLeft:")
        leftGes.direction = UISwipeGestureRecognizerDirection.Left
        leftGes.delegate = self
        swipView.addGestureRecognizer(leftGes)
        contentView.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1.0)
        lineView.backgroundColor = UIColor(red: 217/255.0, green: 217/255.0, blue: 217/255.0, alpha: 1.0)
        if cartItemSelected{
            selectBtn.setBackgroundImage(UIImage(named: "down_checkbox"), forState: UIControlState.Normal)
        }
        else{
            selectBtn.setBackgroundImage(UIImage(named: "nor_checkbox"), forState: UIControlState.Normal)
            
        }
        selectBtn.addTarget(self, action: Selector("selectBtnClick:"), forControlEvents: UIControlEvents.TouchUpInside)
        numberCountView.minNumber = 1
        numberCountView.hideButton(true)
    }
    func swipRight(ges:UIGestureRecognizer){
        self.exitEditModelAnimationed(true)
    }
    func swipLeft(ges:UIGestureRecognizer){
        self.enterEditModelAnimationed(true)

    }
    func enterEditModelAnimationed(animationed:Bool)->Void{
        rightConstraint.constant = deleteButtonWidth.constant
        leftConstraint.constant = -deleteButtonWidth.constant
        if animationed{
            UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.LayoutSubviews, animations:{self.contentView.layoutIfNeeded();self.numberCountView.hideButton(false)}, completion: nil)
        }
        else{
            contentView.layoutIfNeeded()
            self.numberCountView.hideButton(false)
        }
        cellDidEnterEditModel?(indexPath!,true)
    }
    func exitEditModelAnimationed(animationed:Bool)->Void{
        rightConstraint.constant = 0
        leftConstraint.constant = 0
        if animationed{
            UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.LayoutSubviews, animations:{self.contentView.layoutIfNeeded();self.numberCountView.hideButton(true)}, completion: nil)
        }
        else{
            contentView.layoutIfNeeded()
            self.numberCountView.hideButton(true)
        }
        cellDidEnterEditModel?(indexPath!,false)
    }
    func showEdit(editable:Bool)->Void{
        numberCountView.hideButton(!editable)
    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func selectBtnClick(sender:UIButton)->Void{
        cartItemSelected = !cartItemSelected
        cartItemSelectValueChangedEnclose?(indexPath!,cartItemSelected)
    }
}
